
/**
 * 
 */
package pl.puntin.punclient.view.game;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JPanel;

import pl.puntin.punclient.controller.Controller;

/**
 * @author kakus
 *
 */
public final class GameView extends JPanel {

	private javax.swing.JTextArea ChatTextArea;
	private javax.swing.JTextArea WritingTextArea;
	private javax.swing.JButton beginGameButton;
	private javax.swing.JButton blackButton;
	private javax.swing.JButton gameViewButton;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JScrollPane jScrollPane3;
	private javax.swing.JScrollPane jScrollPane4;
	private javax.swing.JTextArea jTextArea2;
	private javax.swing.JButton leavegameButton;
	private javax.swing.JLabel timeLabel;
	private javax.swing.JButton whiteButton;
    private javax.swing.JLabel punForDrawerLabel;

	
	private static final long INTERVAL_BETWEEN_SENDS = 500;
	private static final long DELAY_BEFORE_FIRST_SEND = 2000;
	private final Controller controller;
	private final Timer timer;
	private final DrawPadComponent drawPadComponent;
	private boolean gameCreator = false;
	private boolean imDrowing = false;
    /**
     * Creates new form GameView
     */
    public GameView(Controller controller) {
		this.controller = controller;
		
		drawPadComponent = new DrawPadComponent();	
		timer = new Timer(true);
        initComponents();
    }

    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        WritingTextArea = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        ChatTextArea = new javax.swing.JTextArea();
        gameViewButton = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        timeLabel = new javax.swing.JLabel();
        beginGameButton = new javax.swing.JButton();
        blackButton = new javax.swing.JButton();
        whiteButton = new javax.swing.JButton();
        leavegameButton = new javax.swing.JButton();
        punForDrawerLabel = new javax.swing.JLabel();


        setPreferredSize(new java.awt.Dimension(870, 700));

        drawPadComponent.setMinimumSize(new java.awt.Dimension(640, 480));

        WritingTextArea.setColumns(20);
        WritingTextArea.setRows(5);
        jScrollPane2.setViewportView(WritingTextArea);

        ChatTextArea.setColumns(20);
        ChatTextArea.setEditable(false);
        ChatTextArea.setLineWrap(true);
        ChatTextArea.setRows(5);
        jScrollPane3.setViewportView(ChatTextArea);

        gameViewButton.setText("Wyślij");
        gameViewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gameViewButtonActionPerformed(evt);
            }
        });
        /**
         * Dodaje możliwość wysyłania plików poprzez naciśnięcia enter.
         */
        addKeyListener(new KeyAdapter()
        {
			public void keyPressed(KeyEvent ke) {
                handleInputPressed(ke);
            }

			private void handleInputPressed(KeyEvent ke) {
				if(imDrowing == false)
				{
					int a = ke.getKeyCode();
				    switch(a) {
				        case KeyEvent.VK_ENTER:
				        	chatMessageSend();
				        break;
				    }
				}
			}
        });

        jTextArea2.setColumns(20);
        jTextArea2.setEditable(false);
        jTextArea2.setLineWrap(true);
        jTextArea2.setRows(5);
        jScrollPane4.setViewportView(jTextArea2);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Pozostały czas do końca rundy:");

        timeLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        timeLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        timeLabel.setText("0:50");

        beginGameButton.setText("Rozpocznij Grę");
        beginGameButton.setVisible(false);
        beginGameButton.setEnabled(false);
		beginGameButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				beginGameButtonActionPerformed(evt);
			}
		});
        punForDrawerLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        punForDrawerLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        punForDrawerLabel.setText("Tu będą hasła");
        punForDrawerLabel.setAutoscrolls(true);

        blackButton.setBackground(new java.awt.Color(0, 0, 0));

        whiteButton.setBackground(new java.awt.Color(255, 255, 255));


        leavegameButton.setText("Opuść Grę");
        leavegameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                leavegameButtonActionPerformed(evt);
            }
        });
        blockComponentsForGuessers();
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(drawPadComponent, javax.swing.GroupLayout.DEFAULT_SIZE, 640, Short.MAX_VALUE)
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(gameViewButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane4)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(timeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(beginGameButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(blackButton, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(whiteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, Short.MAX_VALUE))
                        .addComponent(leavegameButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(punForDrawerLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(blackButton, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
                                .addComponent(whiteButton, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(jLabel1)
                            .addGap(18, 18, 18)
                            .addComponent(timeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(punForDrawerLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE))
                        .addComponent(drawPadComponent, javax.swing.GroupLayout.PREFERRED_SIZE, 480, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jScrollPane3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addComponent(beginGameButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                            .addComponent(leavegameButton, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(47, 47, 47)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(gameViewButton, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                    .addContainerGap())
                    );
    }

    private void beginGameButtonActionPerformed(java.awt.event.ActionEvent evt) {
        controller.gameStart();
    }

    private void leavegameButtonActionPerformed(java.awt.event.ActionEvent evt) {
        controller.leaveGame();
    }

    private void gameViewButtonActionPerformed(java.awt.event.ActionEvent evt) {
       this.chatMessageSend();
    }
	
    public void startSendingImage()
	{
		timer.schedule(new TimerTask()
		{
			
			@Override
			public void run()
			{
				controller.sendImage(drawPadComponent.getBufferedImage());
			}
		}, DELAY_BEFORE_FIRST_SEND, INTERVAL_BETWEEN_SENDS);
	}

	public void displayImage(final BufferedImage bufferedImage)
	{
		drawPadComponent.setBufferedImage(bufferedImage);
		drawPadComponent.repaint();
	}
	
	/**
	 * W momencie dołączania sprawdza czy jesteś twórcą gry.
	 * Jeśli tak to masz możliwość rozpoczęcia gry.
	 */
	public void youAreGameCreator(boolean gameCreatore)
	{
		if(true == gameCreatore)
		{		
			beginGameButton.setVisible(true);
			beginGameButton.setEnabled(true);
		}
	}
	
	public void gameStarted()
	{
        beginGameButton.setVisible(false);
        beginGameButton.setEnabled(false);
        punForDrawerLabel.setVisible(false);
	}
	
	public void chatMessageSend()
	{
		controller.sendChatMessage(WritingTextArea.getText());
	}
	
	public void youAreDrowing(String s)
	{
        punForDrawerLabel.setText(s);
        punForDrawerLabel.setVisible(true);
        whiteButton.setEnabled(true);
        blackButton.setEnabled(true);
        drawPadComponent.setEnabled(true);     
	} 
	
	private void blockComponentsForGuessers()
	{
        whiteButton.setEnabled(false);
        blackButton.setEnabled(false);
        drawPadComponent.setEnabled(false);

	}
}

